package application;

import application.dao.DBService;
import application.dao.entity.CompetitionResult;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.*;

import java.util.Date;
import java.util.List;
import java.util.Optional;

public class Athlete_scores_controller extends BaseController {

    @FXML
    public TextField userName;
    @FXML
    public TextField realName;
    @FXML
    public Label competitionTimeLabel;
    @FXML
    public Label scoreLabel;
    @FXML
    public Label rankLabel;
    @FXML
    public Label minusScoreLabel;
    @FXML
    private Menu menu_enroll;

    @FXML
    private Menu menu_main;

    @FXML
    private MenuItem menu_informations_announcements;

    @FXML
    private MenuItem menu_informations_news;

    @FXML
    private Button clickToSearch;

    @FXML
    private Menu menu_person;

    @FXML
    private Menu menu_scores;

    @FXML
    void onClickToSearch(ActionEvent event) {
        System.out.println("search");
        String userNameText = userName.getText();
        String realNameText = realName.getText();
        List<CompetitionResult> competitionResults = DBService.queryAllScore();
        Optional<CompetitionResult> first = competitionResults.stream().filter(item -> {
                    String userName = item.getUserName();
                    String realName = item.getRealName();
                    if (null != userNameText && null != realNameText) {
                        return userName.equals(userNameText) && realName.equals(realNameText);
                    } else {
                        return userName.equals(userNameText) || realName.equals(realNameText);
                    }
                }

        ).findFirst();
        if (first.isPresent()) {
            CompetitionResult result = first.get();
            int i = competitionResults.indexOf(result);
            Date competitionTime = result.getCompetitionTime();
            competitionTimeLabel.setText(BaseUtil.formatDate(competitionTime));
            scoreLabel.setText(result.getScores());
            rankLabel.setText(result.getRank() + "");
            if (i == 0) {
                minusScoreLabel.setText("无");
            } else {
                minusScoreLabel.setText(BaseUtil.between(competitionResults.get(i - 1).getCompetitionTime()
                        , competitionTime));
            }
        } else {
            BaseUtil.info("用户不存在");
        }
    }

    @FXML
    void onCheckMenu_main(ActionEvent event) {

    }

    @FXML
    void onCheckMenu_person(ActionEvent event) {

    }

    @FXML
    void onCheckMenu_enroll(ActionEvent event) {

    }

    @FXML
    void onCheckMenu_scores(ActionEvent event) {

    }

    @FXML
    void onCheckMenu_informations_announcements(ActionEvent event) {

    }

    @FXML
    void onCheckMenu_informations_news(ActionEvent event) {

    }



    @Override
    public void init() {

    }
}